package TomcatClient;


import sun.applet.Main;

import java.io.*;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class tomcat {
    private final int port = 8899;

    public void ServerStart() throws Exception {
        final ServerSocket serverSocket = new ServerSocket(port);
        while (true) {
            final Socket socket = serverSocket.accept();
            Executors.newCachedThreadPool().execute(new Runnable() {//线程池
                @Override
                public void run() {
                    InputStream is = null;
                    try {
                        is = socket.getInputStream();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    byte[] nuf = new byte[1024];
                    try {
                        int len = is.read(nuf);
                        if (len > 0) {
                            String s = new String(nuf, 0, len);
                            System.out.println(s);
                            String usernameXml = "(?<=username=).*?(?=&)";
                            String passwordXml = "(?<=password=).*?(?=H)";
                            String methodNameXml = "(?<=/).*?(?=/)";
                            String username = Macher(usernameXml, s);
                            String password = Macher(passwordXml, s);
                            String methodName = Macher(methodNameXml, s);
                            Class clazz = Class.forName("Login.Login");
                            Method method = clazz.getMethod(methodName, String.class, String.class);
                            boolean bool = (boolean) method.invoke(clazz.newInstance(), username, password);
                            String error = "<html><h1><i>error</i></h1></html>";
                            String success = "<html><h1><i>success</i></h1></html>";
                            if (bool == true) {
                                new sendHtml().sendHtml(success, socket);
                            } else {
                                new sendHtml().sendHtml(error, socket);
                            }

                        }
                        is.close();
                        socket.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }

    private static String Macher(String xml, String s) {
        Pattern compile = Pattern.compile(xml);
        Matcher matcher = compile.matcher(s);
        String temp = null;
        while (matcher.find()) {
            if (temp == null) {
                temp = matcher.group().trim();
            } else {
                break;
            }
        }
        return temp;
    }
}
